<?php


namespace app\api\controller\xilumarket;


use app\common\controller\Api;
use app\common\model\xilumarket\UserAddress AS UserAddressModel;
use function fast\array_get;

class UserAddress extends Api
{
    protected $noNeedRight = ['*'];

    /**
     * 列表
     */
    public function lists(){

        $lists = UserAddressModel::where('user_id',$this->auth->id)
            ->order('default_status','desc')
            ->order('updatetime','desc')
            ->select();
        $this->success('',$lists);
    }


    /**
     * 新增-编辑
     */
    public function set_address(){
        $user_id = $this->auth->id;
        $params = $this->request->post('');

        $validator = new \app\common\validate\xilumarket\UserAddress();
        if(!$validator->scene('add')->check($params)) {
             $this->error($validator->getError());
        }
        $address_model = (new UserAddressModel);
        if($id = array_get($params,'id')){
            $row = $address_model->get($id);
            if($row->user_id != $user_id){
                $this->error('没有权限操作数据');
            }
            $result = $row->allowField(true)->save($params);
            $data = $row;
        }else{
            $params['user_id'] = $user_id;
            $result = $address_model->allowField(true)->save($params);
            $data = $address_model;
        }
        $tip = isset($params['id'])?'保存':'添加';
        if(!$result){
            $this->error($tip.'失败');
        }
        #判断当前是否为默认
        if($params['default_status']== 1){
            $address_model->where('user_id',$user_id)->where('id','<>',$data['id'])->where('default_status',1)->update(['default_status'=>0]);
        }
        $this->success($tip.'成功',$data);


    }

    /**
     * 设为默认
     */
    public function set_default(){
        $params = $this->request->only(['address_id']);
        $row = UserAddressModel::get(['id'=>$params['address_id']]);
        if(!$row) {
            $this->error('地址不存在');
        }
        if($row->default_status == 1) $this->error('地址已设为默认');
        UserAddressModel::where('user_id',$this->auth->id)->where('default_status','1')->update(['default_status'=>0]);
        $row->default_status = 1;
        $row->save();
        $this->success('设置成功');
    }

    /**
     * 删除
     */
    public function del(){
        $user_id = $this->auth->id;
        $params = $this->request->post('');
        $address_model = (new UserAddressModel);
        if($address_id = array_get($params,'address_id')){
            $row = $address_model->get($address_id);
            if(!$row){
                $this->error('地址不存在');
            }
            if($row->user_id != $user_id) {
                $this->error('没有权限操作数据');
            }
            $result = $row->delete();
        }else{
            $this->error('地址参数错误');
        }
        $this->success('删除成功');
    }

    /**
     * 地址详情
     */
    public function detail(){
        $params = $this->request->only(['address_id']);
        if($address_id = array_get($params,'address_id')){
            $row = UserAddressModel::where('id',$address_id)->find()?:[];
            $this->success('',$row);
        }
        $this->error('参数错误');


    }
}